package com.springboot.restful.web;

import com.springboot.restful.model.User;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author liuwei
 * @version 2017/7/15 下午6:28
 * @modified TODO
 * @description 功能描述
 * @see
 * @since 1.0
 */
@RestController
@RequestMapping("/api/user")
public class UserController {
    private Map<Long , User> userMap = new ConcurrentHashMap<Long, User>();

    @RequestMapping(value = "/getList" , method = RequestMethod.GET)
    public List<User> getAll(){
        return new ArrayList<User>(userMap.values());
    }

    @RequestMapping(value = "/save" , method = RequestMethod.POST)
    public String save(@ModelAttribute User user){
        userMap.put(user.getId() , user);
        return "success";
    }
    @RequestMapping(value = "/{id}" , method = RequestMethod.GET)
    public User getById(@PathVariable Long id){
        return userMap.get(id);
    }

    @RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
    public void remove(@PathVariable Long id){
        userMap.remove(id);
    }

    @RequestMapping(value = "/save/{id}" , method = RequestMethod.PUT)
    public String put(@PathVariable Long id){
        User user = new User();
        user.setId(id);
        userMap.put(id , user);
        return "success";
    }
}
